package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.Menu;
import com.clhbolg.domain.bo.MenuBo;
import com.clhbolg.domain.vo.MenuVo;
import com.clhbolg.mapper.MenuMapper;
import com.clhbolg.service.IMenuService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class MenuServiceImpl implements IMenuService {

    private final MenuMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public MenuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<MenuVo> queryPageList(MenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Menu> lqw = buildQueryWrapper(bo);
        Page<MenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<MenuVo> queryList(MenuBo bo) {
        LambdaQueryWrapper<Menu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Menu> buildQueryWrapper(MenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Menu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Menu::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), Menu::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getComponent()), Menu::getComponent, bo.getComponent());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), Menu::getIcon, bo.getIcon());
        lqw.eq(bo.getOrderNum() != null, Menu::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getParentId() != null, Menu::getParentId, bo.getParentId());
        lqw.eq(bo.getIsHidden() != null, Menu::getIsHidden, bo.getIsHidden());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(MenuBo bo) {
        Menu add = BeanUtil.toBean(bo, Menu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(MenuBo bo) {
        Menu update = BeanUtil.toBean(bo, Menu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Menu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
